const app = getApp();
import provList from '../../utils/city.js'
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    cndUrl: app.domainName,
    tipNum:0,
    isDone: 1,
    weather:{tem:'',ty:'',pm:'', zwx:'',wuran:'',tylen:''},
    isX:'',
    isSmall:'',
    shareU:{A:'',B:'',C:''},
    wancheng:{per:'0',id:0, line:0}, //33,3,136 ; 66,6,326
    isold:{a:'',b:'hide'},
    userType:'old',
    address:'',
    fulladdress:'',
    provList,
    userInfo: {prov:'',city:'',provid:'',cityid:''},
  },
  onLoad: function (options) {
    var _this=this;
    wx.request({
      url: app.domainName + 'web/?c=getgroup',
      method: 'POST',
      data: {o:app.globalData.openid}, header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) { 
         console.log(res.data.isold)
         if('new' == res.data.isold){
           _this.setData({isold:{a:'hide',b:''}, userType:'new'});
         }else{
          if('yes' == res.data.queren){
            _this.setData({tipNum:1});
           }
         }
         if(0 == res.data.num){
          _this.setData({wancheng:{per:33,id:3, line:136}});
         }
         if(1 == res.data.num){
          _this.setData({wancheng:{per:66,id:6, line:326}});
         }
         if(res.data.num > 1){
          _this.setData({isDone:2});
         }
         _this.setData({
           'shareU.A':res.data.A,
           'shareU.B':res.data.B,
           'shareU.C':res.data.C,
           fulladdress:res.data.dizhi,
        });
      }
    });

  },
  onShow: function () {

  },
  doclick: function (e) {
    var _this=this;
    var _id=e.currentTarget.dataset.id;
    if('popclose' == _id){
      this.setData({ tipNum: 0});
      return;
    }
    if('closeorder' == _id){
      this.setData({ tipNum: 1});
      return;
    }
    if('dzmodifyok' == _id){
      var _userInfo=this.data.userInfo;
      var _p=_userInfo.prov;
      var _c=_userInfo.city;
      var _pid=_userInfo.provid;
      var _cid=_userInfo.cityid;
      var _address=this.data.address;
      if('' == _p){
        this.popMsg('请选择省');
        return;
      }
      if('' == _c){
        this.popMsg('请选择市');
        return;
      }
      if('' == _address){
        this.popMsg('请输入详细地址');
        return;
      }
      _this.setData({fulladdress: _p+ _c+_address});
      wx.request({
        url: app.domainName + 'web/?c=editUdz',
        method: 'POST',
      data: {op: app.globalData.openid,p:_p,pid:_pid,c:_c,cid:_cid,
        address:_address
        }, header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) { 
            _this.setData({ tipNum: 0});
        }
      });

      return;

    }

    if('dzzhu' == _id){
      wx.redirectTo({url: '/pages/index/index'}) 
      return;
    }
    if('dzwuliu' == _id){
      wx.request({
        url: app.domainName + 'web/?c=getPath',
        method: 'POST',
        data: {op:  app.globalData.openid
        }, header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) { 
        _this.setData({ tipNum: 4, wlPath:res.data});
        }
      });
      return;
    }
    if('dzok' == _id){
      wx.request({
        url: app.domainName + 'web/?c=orderes',
        method: 'POST',
        data: {o:  app.globalData.openid
        }, header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) { 
           _this.setData({ tipNum: 1});
        }
      });
      app.linkflow.sendEvent({"event": "UDE_403BBILBLUS","Click_confirmaddressolds": "老会员邀请成功确认地址按钮"})
      return;
    }
    if('tojuan' == _id){
      this.setData({ tipNum: 5});
      app.linkflow.sendEvent({"event": "UDE_403DKI4STA9","Click_checkcouponsnews": "新会员组队成功查看优惠券"})
      return;
    }
    if('youfuzhi' == _id){
      wx.setClipboardData({
        data: '￥VsW71xtQ4gB￥',
        success (res) {}})
      return;
    }
    if('youtu' == _id){
      this.saveImg(this.data.cndUrl+'img/reg/youhui-nx.png')
      return;
    }
    if('xiudz' == _id){
      wx.request({ url: app.domainName+'web/?c=getdz',data: {o:app.globalData.openid},header: {'content-type': 'application/json' },
              success (res) {
              var _provList=_this.data.provList;
              var _pid=0;
              for(var i=0;i < _provList.length;i++){
                 if(_provList[i].name == res.data.p){
                  _pid=i;
                 }
              }
              _this.setData({
                'userInfo.prov': res.data.p,
                'userInfo.city': res.data.c,
                 address:res.data.dizhi,
                 cityList: _this.data.provList[_pid].arr
              });
              _this.setData({ tipNum: 2});
                
     } });
     return; 
    }
  }, 
  bindchange(e) {
    let act = e.currentTarget.dataset.act
    let val = e.detail.value
    if (act == 'prov') {
        this.setData({
            'userInfo.prov': this.data.provList[val].name,
            'userInfo.provid': this.data.provList[val].id,
            'userInfo.city': '',
            cityList: this.data.provList[val].arr
        })
    } else if (act == 'city') {
        this.setData({
          'userInfo.city': this.data.cityList[val].name,
          'userInfo.cityid': this.data.cityList[val].id
      })
    }
},
  popMsg: function (msg) {
    wx.showModal({content:msg,showCancel:false })
},
bindinput: function (e) {
  var _act=e.currentTarget.dataset.act;
  var _getvalue=e.detail.value;
  if('addressTxt' == _act){
    this.setData({address: _getvalue})
  }
},
saveImg(_url){
  let url = _url;
  //用户需要授权
  wx.getSetting({
    success: (res) => {
      if (!res.authSetting['scope.writePhotosAlbum']) {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success:()=> {
            // 同意授权
            this.saveImg1(url);
          },
          fail: (res) =>{
            console.log(res);
          }
        })
      }else{
        // 已经授权了
        this.saveImg1(url);
      }
    },
    fail: (res) =>{
      console.log(res);
    }
  })   
},
saveImg1(url){
  wx.getImageInfo({
    src: url,
    success:(res)=> {
      let path = res.path;
      wx.saveImageToPhotosAlbum({
        filePath:path,
        success:(res)=> { 
          console.log(res);
        },
        fail:(res)=>{
          console.log(res);
        }
      })
    },
    fail:(res)=> {
      console.log(res);
    }
  })
},
onShareAppMessage: function (res) {
  var _userType=this.data.userType;
  if('old' == _userType){
    app.linkflow.sendEvent({"event": "UDE_403OHNOMMH1","Click_groupshareold": "老会员邀请组队按钮"})
  }else{
    app.linkflow.sendEvent({"event": "UDE_4032HLOSRHI","Click_groupsharenew": "新会员邀请组队按钮"})
  }
  return {
    title: '联盟集结令，精致Girl都已经加入就差你了！',
    path: '/pages/index/index?share='+app.globalData.openid,
    imageUrl:'https://elixir.omnimkt.com/elixir07/img/64.png'
  }
}

})